package com.rritw.webtools.action.tool;

import org.apache.commons.httpclient.Header;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.HtmlUtils;

import com.rritw.webtools.action.IpsAction;
import com.rritw.webtools.common.Const;
import com.rritw.webtools.common.HttpResult;
import com.rritw.webtools.util.HttpClientUtil;
import com.rritw.webtools.util.StringUtil;

@Controller
@Scope("prototype")
public class HttpRequestAction extends IpsAction {

    private static final long   serialVersionUID = 6030221338670150529L;
    private static final Logger log              = Logger.getLogger(HttpRequestAction.class);
    private String              content;
    private String              requestType;
    private String              url;

    private String              contentResult;

    public String index() {
        super.init();
        return SUCCESS;
    }

    public String create() {
        try {
            super.init();
            if (StringUtil.isEmpty(url)) {
                return ERROR;
            }
            long begin = System.currentTimeMillis();
            if ("post".equals(requestType)) {
                if (StringUtil.isEmpty(content)) {
                    contentResult = "The mothod is post, and the post content is needed.";
                    return ERROR;
                }
                HttpResult httpResult = HttpClientUtil.doPost(url, content);
                contentResult = getContentResult(httpResult);
            } else if ("get".equals(requestType)) {
                HttpResult httpResult = HttpClientUtil.doGet(url);
                contentResult = getContentResult(httpResult);
            } else {
                contentResult = "Http request type is only allowed post and get.";
                return ERROR;
            }
            long end = System.currentTimeMillis();
            if (!StringUtil.isEmpty(content)) {
                content = HtmlUtils.htmlEscape(content);
            }
            if (!StringUtil.isEmpty(contentResult)) {
                contentResult = "Time cost:" + (end - begin) + "ms" + Const.LINE_SPLITER + contentResult;
                contentResult = HtmlUtils.htmlEscape(contentResult);
            }
        } catch (Exception e) {
            log.error("Format xml exception happened:" + e.getMessage(), e);
        }
        return SUCCESS;
    }

    private String getContentResult(HttpResult httpResult) {
        StringBuilder result = new StringBuilder();
        result.append("Response Code:").append(httpResult.getCode()).append(Const.LINE_SPLITER);
        result.append("===========================================").append(Const.LINE_SPLITER);
        result.append("Response Header:").append(Const.LINE_SPLITER);
        for (Header header : httpResult.getResponseHeader()) {
            result.append(header.getName()).append(Const.COLON).append(header.getValue()).append(Const.LINE_SPLITER);
        }
        result.append("===========================================").append(Const.LINE_SPLITER);
        result.append("Response Content:").append(Const.LINE_SPLITER);
        result.append(httpResult.getContent());
        return result.toString();
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContentResult() {
        return contentResult;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrl() {
        return url;
    }

    public void setRequestType(String requestType) {
        this.requestType = requestType;
    }

    public String getRequestType() {
        return requestType;
    }

}
